package com.service.impl;

import com.dao.impl.Export_infoDaoImpl;
import com.dao.impl.Import_infoDaoImpl;
import com.dao.impl.Medicine_infoDaoImpl;
import com.pojo.Medicine_info;
import com.service.Medicine_infoService;
import com.util.PageUtil;

import java.util.List;

/**
 * 描述：
 * <p>
 * 作者：jiangjiahao
 * 时间：2023年06月29日 10:32
 */
public class Medicine_infoServiceImpl implements Medicine_infoService {

    private Medicine_infoDaoImpl medicine_infoDao = new Medicine_infoDaoImpl();
    private Import_infoDaoImpl import_infoDao = new Import_infoDaoImpl();
    private Export_infoDaoImpl export_infoDao = new Export_infoDaoImpl();

    /**
     * 分页查询
     * @param index
     * @param size
     * @return
     */
    @Override
    public PageUtil<Medicine_info> pagequery(int index, int size, String query) {
        List<Medicine_info> medicine_infolist = medicine_infoDao.pagequery(index, size, query);
        for (Medicine_info medicine_info : medicine_infolist) {
            int imquantity = 0;
            int exquantity = 0;
            Integer imquan = import_infoDao.queryImportQuantityByMedicineId(medicine_info.getMEDICINE_ID());
            Integer exquan = export_infoDao.queryExportQuantityByMedicineId(medicine_info.getMEDICINE_ID());
            if (imquan != null && imquan != 0){
                imquantity = imquan;
            }
            if (exquan != null && exquan != 0){
                exquantity = exquan;
            }
            int quantity = imquantity-exquantity;
            medicine_info.setQUANTITY(quantity);
        }
        int count = medicine_infoDao.querycount(query);
        int total = (int) Math.ceil((double) count/size);
        PageUtil<Medicine_info> medicine_infoPageUtil =
                new PageUtil <Medicine_info>(index, size, count, total, medicine_infolist);
        return medicine_infoPageUtil;
    }

    /**
     * 查询药品信息总数
     * @return
     */
    @Override
    public Integer querycount(String query) {
        Integer querycount = medicine_infoDao.querycount(query);
        return querycount;
    }

    /**
     * 根据药品id查药品信息
     * @param id
     * @return
     */
    @Override
    public Medicine_info queryMedicineInfoById(int id) {
        return medicine_infoDao.queryMedicineInfoById(id);
    }

    /**
     * 根据药品名称查药品信息
     * @param name
     * @return
     */
    @Override
    public Medicine_info queryMedicineInfoByName(String name) {
        return medicine_infoDao.queryMedicineInfoByName(name);
    }

    /**
     * 根据药品id做删除（假删除）
     * @param id
     */
    @Override
    public void deleteMedicineInfoById(int id) {
        medicine_infoDao.deleteMedicineInfoById(id);
        return;
    }

    /**
     * 添加药品信息
     * @param medicine_info
     */
    @Override
    public Integer addMedicineInfo(Medicine_info medicine_info) {
        Medicine_info medicine_info1 = medicine_infoDao.queryMedicineInfoByName(medicine_info.getNAME());
        if (medicine_info1 == null){
            medicine_infoDao.addMedicineInfo(medicine_info);
            return 0;
        }
        return 1;
    }
}
























